package com.pingancar.creditmanage.service;

import com.pingancar.creditmanage.dao.PAServiceDaoImpl;
import com.pingancar.creditmanage.pojo.PaservicePojo;
import com.pingancar.creditmanage.util.myenum.PAServiceField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2014/9/6 0006.
 */
@Service
public class PAServiceServiceImpl {
    @Autowired
    PAServiceDaoImpl paServiceDao;

    public List<PaservicePojo> findByTag1(String tag1) {
        List<PaservicePojo> result = paServiceDao.findByProperty("tag1", tag1);
        if( result == null ){
            return new ArrayList<PaservicePojo>();
        }
        return result;
    }

    public List<String> getAllTags(String paserviceinfoid) {
        List<String> result = paServiceDao.getAllTags(paserviceinfoid);
        if(result == null){
            result = new ArrayList<String>();
        }
        return result;
    }

    public List<PaservicePojo> queryPAService(List<PAServiceField> paServiceFieldList, List<String> valueList) {
        return paServiceDao.queryPAService(paServiceFieldList,valueList);
    }

    public boolean addPAService(PaservicePojo paServicePojo) {
        List<PaservicePojo> result=paServiceDao.findByProperty("paserviceid", paServicePojo.getPaserviceid());
        if(result == null){
            result=new ArrayList<PaservicePojo>();
        }
        if(!(result.size()>0)){
            return false;
        }
        paServiceDao.save(paServicePojo);
        return true;
    }

    public boolean updatePAService(PaservicePojo paServicePojo) {
        List<PaservicePojo> result=paServiceDao.findByProperty("paserviceid", paServicePojo.getPaserviceid());
        if(result == null){
            return false;
        }
        if(result.size()<1){
            return false;
        }
        PaservicePojo re=result.get(0);
        if(!"".equals(paServicePojo.getTag1())){
            re.setTag1(paServicePojo.getTag1());
        }
        if(!"".equals(paServicePojo.getType())){
            re.setType(paServicePojo.getType());
        }
        if(!"".equals(paServicePojo.getCreditcost())) {
            re.setCreditcost(paServicePojo.getCreditcost());
        }
        if(!"".equals(paServicePojo.getAmount())) {
            re.setAmount(paServicePojo.getAmount());
        }
        if(!"".equals(paServicePojo.getLeftnum())) {
            re.setLeftnum(paServicePojo.getLeftnum());
        }
        paServiceDao.update(re);
        return true;
    }

    public boolean deletePAService(PaservicePojo paServicePojo) {
        List<PaservicePojo> result=paServiceDao.findByProperty("paserviceid", paServicePojo.getPaserviceid());
        if(result == null){
            return false;
        }
        if(result.size()<1){
            return false;
        }
        PaservicePojo re=result.get(0);
        paServiceDao.delete(re);
        return true;
    }
}
